<template>
  <div class="app-container">
    <el-col :xs="24" :sm="24" :md="12" :lg="10" :xl="10" style="margin-bottom: 10px">
      <el-card class="box-card">
        <el-tree
          ref="tree"
          :data="data1"
          node-key="id"
          accordion
          default-expand-all
          :show-checkbox="true"
          @check-change="nodeClick"
        >
          <span slot-scope="{ node, data }" class="custom-tree-node">
            <span>{{ node.label }}</span>
            <span>
              <i class="el-icon-plus" @click="() => append(node, data)" />
              <i class="el-icon-minus" @click="() => remove(node, data)" />
            </span>
          </span>
        </el-tree>
      </el-card>
    </el-col>
    <el-col :xs="24" :sm="24" :md="12" :lg="14" :xl="14">
      <el-card class="box-card">
        few
      </el-card>
    </el-col>
  </div>
</template>
<script>
let id = 1000

export default {
  data() {
    const dataM = [{
      id: 1,
      label: '非金融企业债务融资工具',
      children: [{
        id: 2,
        label: '立项'
      }, {
        id: 3,
        label: '额度管理'
      }, {
        id: 4,
        label: '注册管理',
        children: [{
          id: 5,
          label: '注册'
        }, {
          id: 6,
          label: '加团'
        }]
      }]
    }, {
      id: 7,
      label: '结构化融资业务',
      children: [{
        id: 8,
        label: '意向立项'
      }, {
        id: 9,
        label: '出账申请'
      }, {
        id: 10,
        label: '缴款申请'
      }, {
        id: 11,
        label: '后续管理',
        children: [{
          id: 12,
          label: '产品变更'
        }, {
          id: 13,
          label: '提前还款'
        }, {
          id: 14,
          label: '含权管理'
        }, {
          id: 15,
          label: '本息兑付'
        }]
      }]
    }]
    return {
      data1: JSON.parse(JSON.stringify(dataM))
    }
  },

  methods: {
    append(node, data) {
      this.$refs.tree.setCheckedNodes([{ id: data.id }])
      this.$prompt('请输入节点名', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({ value }) => {
        const newChild = { id: id++, label: value, children: [] }
        if (!data.children) {
          this.$set(data, 'children', [])
        }
        data.children.push(newChild)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消输入'
        })
      })
    },

    remove(node, data) {
      this.$confirm('是否删除该节点?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const parent = node.parent
        const children = parent.data.children || parent.data
        const index = children.findIndex(d => d.id === data.id)
        children.splice(index, 1)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      })
    },
    nodeClick(target, selected, childrenSelected) {
      if (!selected) return
      debugger
      this.$message({
        type: 'success',
        message: target.label
      })
    }
  }
}
</script>

<style>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding-right: 8px;
}
</style>
